package com.yookey.controller;

import com.yookey.common.constant.ResponseMessage;
import com.yookey.common.result.Result;
import com.yookey.service.UploadService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author yww
 * @date 2020/9/29
 */
@RestController
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private UploadService uploadService;


    @ApiOperation(value = "头像上传,保存在本地")
    @PostMapping
    public Result uploadAvatar(MultipartFile multipartFile) throws Exception {
        if(multipartFile == null) {
            return Result.error(ResponseMessage.PARAMERROR);
        }
        String imgUrl = uploadService.uploadAvatar(multipartFile);
        return Result.success(ResponseMessage.FILE_UPLOAD_SUCCESS,imgUrl);
    }

    //上传头像的方法
    @ApiOperation(value = "头像上传,保存在阿里云oss")
    @PostMapping("/oss")
    public Result uploadOssFile(MultipartFile multipartFile) {
        //获取上传文件  MultipartFile
        //返回上传到oss的路径
        String url = uploadService.uploadOssAvatar(multipartFile);
        return Result.success(ResponseMessage.FILE_UPLOAD_SUCCESS,url);
    }


}
